<template>
  <div>
    <form class="card comment-form" v-if="user">
      <div class="card-block">
        <textarea v-model="comment.body" commentclass="form-control" placeholder="Write a comment..." rows="3"></textarea>
      </div>
      <div class="card-footer">
        <img :src="user && user.image" class="comment-author-img" />
        <button class="btn btn-sm btn-primary" @click="postComments">
        Post Comment
        </button>
      </div>
    </form>

    <div
      class="card"
      v-for="comment in comments"
      :key="comment.id"
    >
      <div class="card-block">
        <p class="card-text">{{ comment.body }}</p>
      </div>
      <div class="card-footer">
        <nuxt-link class="comment-author" :to="{
          name: 'profile',
          params: {
            username: comment.author.username
          }
        }">
          <img :src="comment.author.image" class="comment-author-img" />
        </nuxt-link>
        &nbsp;
        <nuxt-link class="comment-author" :to="{
          name: 'profile',
          params: {
            username: comment.author.username
          }
        }">
          {{ comment.author.username }}
        </nuxt-link>
        <span class="date-posted">{{ comment.createdAt | date('MMM DD, YYYY') }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getComments, postComments } from '@/api/article'
import { mapState } from "vuex";

export default {
  name: 'ArticleComments',
  props: {
    article: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      comments: [],
      comment: {
        "body": ""
      }
    }
  },
  computed:{
    ...mapState(['user']),
  },
  mounted () {
    this.getComments();
  },
  methods: {
    async getComments() {
      const { data } = await getComments(this.article.slug)
      this.comments = data.comments
    },
    async postComments() {
       await postComments(this.article.slug, this.comment)
       this.comment.body = ""
       this.getComments()
    }
  }

}
</script>

<style>

</style>
